package com.bank.abmock.controller;


import com.bank.abmock.Utils.JsonUtils;
import com.bank.abmock.mapper.RouteMapperCenter;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Controller
@RequiredArgsConstructor
@Slf4j
public class BezelController {
    //    private final RouteConfig routeConfig;
    private final ObjectMapper objectMapper;
    private final XmlMapper xmlMapper;
    private final ResourceLoader resourceLoader;

    @ResponseBody
    @RequestMapping(value = "/json", produces = {"application/json;charset=UTF-8"})
    public Object jsonList() {
        try {
            String pattern = "classpath:JsonTemplate/*.json";
            Resource[] jsonResources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources(pattern);
            List<String> list = new LinkedList();
            Map<String, String> jsonRequestMap = new LinkedHashMap<>();
            for (Resource resource : jsonResources) {
                String nameWithoutSuffix = resource.getFilename().substring(0, resource.getFilename().lastIndexOf('.'));
                jsonRequestMap.put(nameWithoutSuffix,nameWithoutSuffix);
                list.add(nameWithoutSuffix);
            }
            return jsonRequestMap;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "读取列表失败";
    }

    @Autowired
    RouteMapperCenter routeMapperCenter;

    /*@ResponseBody
    @RequestMapping(value = "/json/{templateName}", produces = {"application/json;charset=UTF-8"})
    public Object jsonIndex(@PathVariable(required = true) String templateName,
                            @RequestParam(required = false) Map<String, Object> map) {
        try {
            File jsonFile = ResourceUtils.getFile("classpath:JsonTemplate/" + templateName + ".json");
            if (jsonFile != null) {
                JsonNode root = objectMapper.readTree(jsonFile);
                return JsonUtils.generateValues(root, map).toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "json模板不存在";
    }*/

    @ResponseBody
    @RequestMapping(value = "/json/{subPath}", produces = {"application/json;charset=UTF-8"})
    public Object jsonIndex(@PathVariable(required = false) String subPath,
                            @RequestBody Map<String, Object> map) {

        Object response = routeMapperCenter.process(subPath, map);
        if(response != null){
            return response;
        }
        return "路由未配置";
    }

    @ResponseBody
    @RequestMapping(value = "/xml/{subPath}", produces = {"application/json;charset=UTF-8"})
    public Object xmlIndex(@PathVariable(required = false) String subPath,
                            @RequestBody Map<String, Object> map) {

        Object response = routeMapperCenter.process(subPath, map);
        if(response != null){
            return response;
        }
        return "路由未配置";
    }

//    @ResponseBody
//    @RequestMapping(value = "/xml/{templateName}", produces = {"application/xml;charset=UTF-8"})
//    public Object xmlIndex(@PathVariable(required = false) String templateName,
//                           @RequestParam(required = false) Map<String, Object> map) {
//        try {
//            File jsonFile = ResourceUtils.getFile("classpath:JsonTemplate/" + templateName + ".json");
//            if (jsonFile != null) {
//                JsonNode root = objectMapper.readTree(jsonFile);
//
//                return xmlMapper.writer().withRootName("root").writeValueAsString(JsonUtils.generateValues(root, map));
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return "json模板不存在";
//    }

    @ResponseBody
    @RequestMapping(value = "/wke/{templateName}", produces = {"application/xml;charset=UTF-8"})
    public Object wkeIndex(@PathVariable(required = false) String templateName,
                           @RequestParam(required = false) Map<String, Object> map) {
        return "尚未支持";
    }
}
